using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
#if SILVERLIGHT
using System.ComponentModel;
using System.Collections.Specialized;
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using System.Collections;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using AbstraX.BuildWizard;
using System.Windows.Threading;
using UIObjects;

namespace AbstraX.Contracts
{
    public delegate void ExceptionHandler(object sender, ExceptionEventArgs e);

    public class ExceptionEventArgs : EventArgs
    {
        public Exception Exception { get; set; }

        public ExceptionEventArgs()
        {
        }

        public ExceptionEventArgs(Exception ex)
        {
            this.Exception = ex;
        }
    }

    public class ImageInvokeOperation
    {
        private byte[] imageData;
#if SILVERLIGHT
		private DispatcherTimer timer;
#endif        
        public event EventHandler OnImageCompleted;
        public event ExceptionHandler ExceptionOccured;
        public bool IsComplete;
        public Exception Exception { get; set; }

        public ImageInvokeOperation(byte[] imageData)
        {
#if SILVERLIGHT

            timer = new DispatcherTimer();

            timer.Tick += (sender, e) =>
            {
                timer.Stop();
                SetCompleted(imageData);
            };

            timer.Interval = new TimeSpan(1);
            timer.Start(); 
#endif
        }

        public ImageInvokeOperation()
        {
            this.IsComplete = false;
        }

        public ImageInvokeOperation(Exception ex)
        {
            this.Exception = ex;
            this.IsComplete = true;
        }

        public byte[] ImageData
        {
            get
            {
                return imageData;
            }
        }

        public bool HasException
        {
            get
            {
                return this.Exception != null;
            }
        }

        public void SetException(Exception ex)
        {
            this.Exception = ex;
            ExceptionOccured(this, new ExceptionEventArgs(ex));
        }

        public void SetCompleted(byte[] imageData)
        {
            this.imageData = imageData;

            this.IsComplete = true;
            OnImageCompleted(this, EventArgs.Empty);
        }
    }

    public class BuildInvokeOperation
    {
        public event EventHandler OnBuildCompleted;
        public event ExceptionHandler ExceptionOccured;
        public DateTime BuildStarted;
        public bool IsComplete;
        public Exception Exception { get; set; }

        public BuildInvokeOperation()
        {
            this.IsComplete = false;
        }

        public BuildInvokeOperation(Exception ex)
        {
            this.Exception = ex;
            this.IsComplete = true;
        }

        public bool HasException
        {
            get
            {
                return this.Exception != null;
            }
        }

        public void SetException(Exception ex)
        {
            this.Exception = ex;
            ExceptionOccured(this, new ExceptionEventArgs(ex));
        }

        public void SetCompleted()
        {
            this.IsComplete = true;
            OnBuildCompleted(this, EventArgs.Empty);
        }
    }

    public class EventsStartOperation
    {
        public event EventHandler OnEventsStarted;
        public event ExceptionHandler ExceptionOccured;
        public DateTime EventsStarted;
        public bool IsComplete;
        public Exception Exception { get; set; }

        public EventsStartOperation()
        {
            this.IsComplete = false;
        }

        public EventsStartOperation(Exception ex)
        {
            this.Exception = ex;
            this.IsComplete = true;
        }

        public bool HasException
        {
            get
            {
                return this.Exception != null;
            }
        }

        public void SetException(Exception ex)
        {
            this.Exception = ex;
            ExceptionOccured(this, new ExceptionEventArgs(ex));
        }

        public void SetCompleted()
        {
            this.IsComplete = true;
            OnEventsStarted(this, EventArgs.Empty);
        }
    }

#if SILVERLIGHT
    public interface IRootsCollection : ICollection, INotifyCollectionChanged, INotifyPropertyChanged
#else
    public interface IRootsCollection : ICollection
#endif
    {
    }

    public interface IProviderClientService
    {
        IRootsCollection ProviderRoots { get; }
        ImageInvokeOperation LoadImageForFolder(string itemTypeName, string keyName);
        ImageInvokeOperation LoadImageForItemType(string itemTypeName);
        ImageInvokeOperation LoadImageForUrl(string itemTypeName, string url);
        ImageInvokeOperation LoadImageForUrl(string url);
        T GenerateItemFromID<T>(string id) where T : IBase;
        BuildInvokeOperation Build(string id);
        bool BuildInProgress { get; }
        ILog Log { get; }
        IWizardContainer WizardContainer { get; set; }
    }
}

//#endif